package com.gurun.codes.business.setting;

import android.app.ProgressDialog;
import android.content.Context;
import android.content.Intent;
import android.databinding.DataBindingUtil;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;

import com.afollestad.materialdialogs.DialogAction;
import com.afollestad.materialdialogs.MaterialDialog;
import com.gurun.base.DebounceObservableTransformer;
import com.gurun.base.utils.CommonUtils;
import com.gurun.base.utils.ToastManager;
import com.gurun.codes.App;
import com.gurun.codes.R;
import com.gurun.codes.base.BaseAppCompatActivity;
import com.gurun.codes.business.login.LoginActivity;
import com.gurun.codes.business.login.LoginActivityContract;
import com.gurun.codes.databinding.ActivitySettingBinding;
import com.gurun.codes.databinding.DialogLoginBinding;
import com.gurun.model.bean.event_bean.LoginSuccessEvent;
import com.gurun.model.bean.user.LoginResultBean;
import com.jakewharton.rxbinding2.view.RxView;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import io.reactivex.functions.Consumer;

/**
 * 设置页面
 */
public class SettingActivity extends BaseAppCompatActivity {

    private ActivitySettingBinding mBinding;
    public static void startActivity(Context context) {
        context.startActivity(new Intent(context, SettingActivity.class));
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        mBinding = DataBindingUtil.setContentView(this, R.layout.activity_setting);
        initView();
        initDatas();
        initEvent();
        if (!EventBus.getDefault().isRegistered(this)){
            EventBus.getDefault().register(this);
        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (EventBus.getDefault().isRegistered(this)){
            EventBus.getDefault().unregister(this);
        }
    }

    @Override
    protected void initView() {
        String userName = App.getInstance().getUserName();
        if (!TextUtils.isEmpty(userName)) {
            mBinding.tvAccount.setText(userName);
        }
        String version = CommonUtils.getVersionName(mContext);
        if (!TextUtils.isEmpty(version)) {
            mBinding.tvVersion.setText(version);
        }
    }

    @Override
    protected void initDatas() {

    }

    @Override
    protected void initEvent() {
//        RxView.clicks(mBinding.llItemOne)
//                .compose(new DebounceObservableTransformer<Object>())
//                .compose(this.bindToLifecycle())
//                .subscribe(new Consumer<Object>() {
//                    @Override
//                    public void accept(Object o) throws Exception {
//                        LoginActivity.startActivity(mContext);
//                    }
//                });

        RxView.clicks(mBinding.btnLoguot)
                .compose(new DebounceObservableTransformer<Object>())
                .compose(this.bindToLifecycle())
                .subscribe(new Consumer<Object>() {
                    @Override
                    public void accept(Object o) throws Exception {
                        //TODO 需要调用后台logout接口
                        App.getInstance().logOut();
                        LoginActivity.startActivity(mContext);
                    }
                });
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onLoginSuccess(LoginSuccessEvent event) {
        if (event != null) {
            setupAccount(event.getNickName());
        }
    }

    private void setupAccount(String account) {
        if (!TextUtils.isEmpty(account)) {
            mBinding.tvAccount.setText(account);
        }
    }

}
